//
//  ShufflingFigureIdentifyView.swift
//  NetWork
//
//  Created by Delin Meng 孟德林 on 2018/10/31.
//  Copyright © 2018 Delin Meng 孟德林. All rights reserved.
//

import UIKit

class ShufflingFigureIdentifyView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        configureUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configureUI()
    }

    private var collectionView:UICollectionView?

    /// 当前的page
    public var currentPage:Int = 0 {

        didSet{
            collectionView?.reloadData()
        }

    }


    /// 总page
    public var  numberOfPages:Int = 0 {

        didSet{
            collectionView?.reloadData()
        }

    }


    private func configureUI() {

        let cellWidth = (bounds.size.width - CGFloat(numberOfPages - 1) * ShufflingFigureComment.IdentifierCellSpacing) / CGFloat(numberOfPages)
        let shufflingFigureLayout = ShufflingFigureLayout()
        shufflingFigureLayout.itemSize = CGSize.init(width: cellWidth, height: ShufflingFigureComment.IdentifierCellHeight)
        let collectV = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: shufflingFigureLayout)
        collectionView = collectV
        collectionView?.dataSource = self
        collectionView?.delegate = self
        collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: ShufflingFigureComment.CellIdentifier)
        addSubview(collectionView!)

    }

    override func layoutSubviews() {
        super.layoutSubviews()
        collectionView?.snp.makeConstraints { (make) in
            make.left.right.top.bottom.equalToSuperview()
        }
    }

    deinit {
        print("ShufflingFigureIdentifyView is release")
    }

}

extension ShufflingFigureIdentifyView: UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cellWidth = (bounds.size.width - CGFloat(numberOfPages - 1) * ShufflingFigureComment.IdentifierCellSpacing) / CGFloat(numberOfPages)
        return CGSize.init(width: cellWidth, height: ShufflingFigureComment.IdentifierCellHeight)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return ShufflingFigureComment.IdentifierCellSpacing
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return numberOfPages
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:ShufflingFigureComment.CellIdentifier , for: indexPath)
        if indexPath.row == currentPage % numberOfPages {
            cell.backgroundColor = ShufflingFigureComment.IdentifierCellSelectedColor
        }else{
            cell.backgroundColor = ShufflingFigureComment.IdentifierCellNormalColor
        }
        cell.clipsToBounds = true
        cell.layer.cornerRadius = 1
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ShufflingFigureComment.CellIdentifier, for: indexPath)
        if indexPath.row == currentPage {
            cell.backgroundColor = ShufflingFigureComment.IdentifierCellSelectedColor
        }else{
            cell.backgroundColor = ShufflingFigureComment.IdentifierCellNormalColor
        }
    }
}


